package com.situ.thread.juc;


import java.util.concurrent.TimeUnit;

public class VolatileDemo {
    private volatile static int num = 0;

    public static void main(String[] args) { // main
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (num == 0){
                    //println 代码影响了编译器的优化,要保守处理，重新读取
                    //System.out.println("VolatileDemo.run num :" + num);
                }
            }
        }).start();

        // Thread.sleep(1000);
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        num = 1;
        System.out.println(num);
    }
}
